import cv2
import pickle
import numpy as np
import time

# 动物分类器
animal_cascade = pickle.load(open('model.sav', 'rb'))

# 读取图片
camera = cv2.VideoCapture(0)

while True:

    ret, frame = camera.read()

    image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (128, 128))
    image = image.reshape(1, -1)
    prediction = animal_cascade.predict(image)

    if prediction == 0:
        # 在图片上写上文字,超大字体加粗
        cv2.putText(frame, "pig", (10, 50),
                    cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)
        cv2.imwrite("photo.jpg", frame)

        print("当前是猪")
    else:
        print("当前是鸡")

    time.sleep(0.5)
